<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_akun extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function get_data(){
		$this->db->flush_cache();

		$this->db->from('akun');
		
		$this->db->order_by('kode_akun', 'ASC');
		
		$query = $this->db->get();
		
		foreach($query->result() as $r){
			$data[$r->parent][] = $r;
		}
		
		$menu = $this->akun_list($data);

		//print_r($menu);
		return $menu;
	}
	
	function akun_list($data, $parent = 0 ){
		if(isset($data[$parent])){
			$result = array();
			foreach($data[$parent] as $v){
				$node = array();
				$node['kode_akun'] = $v->kode_akun;
				$node['deskripsi'] = $v->deskripsi;
				$node['iconCls'] = $v->tipe == 'H' ? 'icon-akun_header' : '';
				$node['children'] = $this->akun_list($data, $v->kode_akun);
								
				if($node['children']){
					$node['state'] = 'opened';
				}
				
				array_push($result, $node);
			}
			return $result;
			
		}else{
			return false;
		}
	}
	
	function get_data_edit($kode){
		$this->db->where('kode_akun', $kode);
		return $this->db->get('akun');
	}
	
	public function kode_exist($kode_akun){
		$this->db->where('kode_akun', $kode_akun);
		$res = $this->db->get('akun');

		return ($res->num_rows() > 0);
	}

	function InsertOnDb($data){
		//query insert data	
		$this->db->set('kode_akun', $data['kode_akun']);
        $this->db->set('parent', $data['parent']);
        $this->db->set('tipe', $data['tipe']);
        $this->db->set('deskripsi', $data['deskripsi']);
        $this->db->set('status', '1');

		$result = $this->db->insert('akun');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}

	function UpdateOnDb($data){
		//query insert data	
		$this->db->set('kode_akun', $data['kode_akun']);
        $this->db->set('parent', $data['parent']);
        $this->db->set('tipe', $data['tipe']);
        $this->db->set('deskripsi', $data['deskripsi']);
        $this->db->set('status', '1');

        $this->db->where('kode_akun', $data['kode']);

		$result = $this->db->update('akun');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
}

?>